<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <PageTools title="总记录数据20条">
          <!-- v-if="
              $store.state.user.userInfo.roles.points.includes('import_excel')
            " -->
          <el-button
            v-allow="'import_excel'"
            type="warning"
            size="small"
            @click="$router.push('/import')"
          >excel导入</el-button>
          <!-- v-if="
              $store.state.user.userInfo.roles.points.includes('export_excel')
            " -->
          <el-button
            v-allow="'export_excel'"
            type="danger"
            size="small"
            @click="exportExcel"
          >excel导出</el-button>
          <el-button
            type="primary"
            size="small"
            @click="showDialog = true"
          >新增员工</el-button>
        </PageTools>
      </el-card>
      <el-card style="margin-top: 10px">
        <el-table border :data="list">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="头像">
            <template slot-scope="scope">
              <image-holder :src="scope.row.staffPhoto" />
            </template>
          </el-table-column>
          <el-table-column label="工号" sortable prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template slot-scope="scope">
              {{ formta(scope.row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" sortable prop="timeOfEntry" />
          <!-- <el-table-column label="账户状态" prop="" /> -->
          <el-table-column label="操作" width="280">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="small"
                @click="$router.push('/employees/detail?id=' + scope.row.id)"
              >查看</el-button>
              <el-button
                type="text"
                size="small"
                @click="hAssignRole(scope.row.id)"
              >分配角色</el-button>
              <el-button
                type="text"
                size="small"
                @click="hDel(scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <el-pagination
            :total="total"
            :current-page="page"
            :page-size="size"
            :page-sizes="[5, 10, 20, 50, 100, 200, 300]"
            layout="sizes,prev, pager, next"
            @current-change="hCurrentChange"
            @size-change="hSizeChange"
          />
        </el-row>
      </el-card>
    </div>

    <el-dialog
      title="新增"
      :close-on-press-escape="false"
      :visible.sync="showDialog"
      @close="hClose"
    >
      <emp-dialog ref="resForm" @success="success" />
    </el-dialog>

    <el-dialog :visible.sync="showAssig">
      <AssignRole v-if="showAssig" :cur-id="curId" @success="hSuccessAssig" />
    </el-dialog>
  </div>
</template>
<script>
import EmpDialog from '@/views/employees/EmpDialog'
import { getEmployeeList, delEmployee } from '@/api/employees'
import { type_map } from '@/constant/index'
import AssignRole from './AssignRole'
export default {
  components: { EmpDialog, AssignRole },
  data() {
    return {
      list: [],
      page: 1,
      size: 10,
      total: 0,
      showDialog: false,
      showAssig: false,
      curId: ''
    }
  },
  created() {
    this.loadEmployee()
  },
  methods: {
    // 接收到子组件的通知之后
    hSuccessAssig() {
      this.showAssig = false
      this.$message.success('修改成功')
    },
    hAssignRole(id) {
      this.showAssig = true
      this.curId = id
    },
    formatData(rows) {
      // 1. 表头
      // 英文转换成中文
      const mapObj = {
        id: '编号',
        password: '密码',
        mobile: '手机号',
        username: '姓名',
        timeOfEntry: '入职日期',
        formOfEmployment: '聘用形式',
        correctionTime: '转正日期',
        workNumber: '工号',
        departmentName: '部门',
        staffPhoto: '头像地址'
      }
      //   const enHeader = Object.keys(rows[0])
      const header = Object.values(mapObj)
      // 2. 内容
      const data = rows.map((obj) => {
        obj.formOfEmployment = type_map[obj.formOfEmployment] || '未知'
        return Object.values(obj)
      })
      return { header, data }
    },
    async exportExcel() {
      // 1. 发请求拿数据
      const res = await getEmployeeList(this.page, this.size)
      // 2. 对数据进行格式转换
      const { header, data } = this.formatData(res.data.rows)
      console.log('员工信息', res.data.rows)
      import('@/vendor/Export2Excel').then((excel) => {
        // excel表示导入的模块对象
        console.log(excel)
        excel.export_json_to_excel({
          header, // 表头 必填
          data, // 具体数据 必填
          filename: '员工信息', // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: 'xlsx' // 生成的文件类型
        })
      })
    },
    hClose() {
      this.$refs.resForm.resFormData()
    },
    success() {
      // 关闭弹窗
      this.showDialog = false
      // 跳转到最后一页
      this.total++
      this.page = Math.ceil(this.total / this.size)
      // 重新加载
      this.loadEmployee()
    },
    async doDel(id) {
      // 1.调用接口做删除
      await delEmployee(id)
      // 判断如果当前删除是表格中的最后一个数据,要页码-1
      if (this.list.length === 1) {
        if (this.page > 1) {
          this.page--
        }
      }
      // 2. 提示用户,删除成功
      this.$message.success('删除成功')
      // 3. 重新加载
      this.loadEmployee()
    },
    // 点击删除
    hDel(id) {
      // 提示确定要删除吗?
      this.$confirm('确定要删除吗?', '系统提示', {
        type: 'warning'
      })
        .then(() => {
          this.doDel(id)
        })
        .catch(() => {})
    },
    // 更新每页条数
    hSizeChange(curPagesize) {
      console.log(curPagesize)
      // 1. 更新页码
      this.size = curPagesize
      this.page = 1
      // 2.更新数据
      this.loadEmployee()
    },
    // 点击每页
    hCurrentChange(curPage) {
      console.log(curPage)
      // 1. 更新页码
      this.page = curPage
      // 2. 重发请求
      this.loadEmployee()
    },
    formta(type) {
      return type_map[type] || '未知'
    },
    async loadEmployee() {
      try {
        const res = await getEmployeeList(this.page, this.size)
        console.log(res)
        this.list = res.data.rows
        this.total = res.data.total
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>
